package cn.itcast.bos.service.take_delivery.impl;

import cn.itcast.bos.dao.base.WayBillRepository;
import cn.itcast.bos.dao.index.WayBillIndexRepository;
import cn.itcast.bos.domain.take_delivery.WayBill;
import cn.itcast.bos.service.take_delivery.WayBillService;
import org.apache.commons.lang3.StringUtils;
import org.apache.lucene.search.TermQuery;
import org.elasticsearch.index.query.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional
public class WayBillServiceImlp implements WayBillService {

    @Autowired
    WayBillRepository wayBillRepository;

    @Autowired
    WayBillIndexRepository wayBillIndexRepository;

    @Override
    public void save(WayBill model) {
        wayBillRepository.save(model);
    }

    @Override
    public WayBill findByWayBillNum(String wayBillNum) {
        WayBill wayBill = wayBillRepository.findByWayBillNum(wayBillNum);
        return wayBill;
    }

    @Override
    public Page<WayBill> findPageData(WayBill model, PageRequest request) {
        if (StringUtils.isBlank(model.getWayBillNum())
                &&StringUtils.isBlank(model.getSendAddress())
                &&StringUtils.isBlank(model.getRecAddress())
                &&StringUtils.isBlank(model.getGoodsType())
                &&(model.getSignStatus()==null || model.getSignStatus()==0)){
            //无条件查询，查询数据库
            return wayBillRepository.findAll(request);

        }else {
            BoolQueryBuilder query = new BoolQueryBuilder();
            if (StringUtils.isNoneBlank(model.getWayBillNum())){
                //运单号查询
                QueryBuilder queryBuilder = new TermQueryBuilder("wayBillNum", model.getWayBillNum());
                query.must(queryBuilder);
            }
            if (StringUtils.isNoneBlank(model.getSendAddress())) {
                // 发货地 模糊查询
                // 情况一： 输入"北" 是查询词条一部分， 使用模糊匹配词条查询
                QueryBuilder wildcardQuery = new WildcardQueryBuilder(
                        "sendAddress", "*" + model.getSendAddress() + "*");

                // 情况二： 输入"北京市海淀区" 是多个词条组合，进行分词后 每个词条匹配查询
                QueryBuilder queryStringQueryBuilder = new QueryStringQueryBuilder(
                        model.getSendAddress()).field("sendAddress").defaultOperator(QueryStringQueryBuilder.Operator.AND);
//                        .defaultOperator(Operator.AND);

                // 两种情况取or关系
                BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
                boolQueryBuilder.should(wildcardQuery);
                boolQueryBuilder.should(queryStringQueryBuilder);

                query.must(boolQueryBuilder);
            }
            if (StringUtils.isNoneBlank(model.getRecAddress())) {
                // 收货地 模糊查询
                QueryBuilder wildcardQuery = new WildcardQueryBuilder(
                        "recAddress", "*" + model.getRecAddress() + "*");
                query.must(wildcardQuery);
            }
            if (StringUtils.isNoneBlank(model.getSendProNum())) {
                // 速运类型 等值查询
                QueryBuilder termQuery = new TermQueryBuilder("sendProNum",
                        model.getSendProNum());
                query.must(termQuery);
            }
            if (StringUtils.isNoneBlank(model.getSendProNum())) {
                // 速运类型 等值查询
                QueryBuilder termQuery = new TermQueryBuilder("sendProNum",
                        model.getSendProNum());
                query.must(termQuery);
            }
            if (model.getSignStatus() != null && model.getSignStatus() != 0) {
                // 签收状态查询
                QueryBuilder termQuery = new TermQueryBuilder("signStatus",
                        model.getSignStatus());
                query.must(termQuery);
            }

            SearchQuery searchQuery = new NativeSearchQuery(query);
            searchQuery.setPageable(request); // 分页效果
            // 有条件查询 、查询索引库
            return wayBillIndexRepository.search(searchQuery);


        }
    }
}
